<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="人员姓名" prop="personName">
        <el-input v-model="queryParams.personName" placeholder="请输入人员姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="报警规则类型" prop="alarmType">
        <el-select v-model="queryParams.alarmType" placeholder="请选择报警规则类型" clearable size="small">
          <el-option v-for="dict in dict.type.alarm_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="事件是否结束" prop="eventFinish">
        <el-select v-model="queryParams.eventFinish" placeholder="请选择事件是否结束" clearable size="small">
          <el-option v-for="dict in eventFinishOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="事件时间">
        <el-date-picker v-model="queryParams.daterange" type="datetimerange" align="right" size="small" unlink-panels
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['alarm:event:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['alarm:event:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column type="index" label="序号" width="60" align="left" />
      <el-table-column label="所属部门" align="left" prop="deptName" />
      <el-table-column label="排班" align="left" prop="workPlanName" />
      <el-table-column label="班组" align="left" prop="workTeamName" />
      <!-- <el-table-column label="车牌" align="left" prop="carNum" /> -->
      <el-table-column label="人员姓名" align="left" prop="personName" />

      <el-table-column label="报警规则类型" align="left" prop="alarmType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_type" :value="scope.row.alarmType" />
        </template>
      </el-table-column>

      <el-table-column label="事件开始时间" align="left" prop="eventStart" width="180" />

      <el-table-column label="事件结束时间" align="left" prop="eventEnd" width="180" />

      <el-table-column label="是否结束" align="left" prop="eventFinish">
        <template slot-scope="scope">
          <span>{{
            eventFinishOptions.find((v) => v.dictValue == scope.row.eventFinish)
              .dictLabel
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="事件描述" align="left" prop="eventDesc" /> -->
      <el-table-column label="持续时长 " align="left" prop="persistTime">
        <template slot-scope="scope">
          <span>{{ scope.row.persistTime }}分钟</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="!(
              scope.row.alarmType == 'FACE_COMPARE_ERROR' ||
              scope.row.alarmType == 'LOCK_ERROR'
            )
            " size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['alarm:event:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['alarm:event:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 处理 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="处理内容" prop="dealContent">
          <el-input v-model="form.dealContent" type="textarea" placeholder="请输入处理信息" class="dealContent" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="buttonLoading" @click="submitForm">处 理</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 报警事件详情对话框 -->
    <event-detail :visible.sync="eventDetailVisible" :eventItem="eventItem" />
  </div>
</template>

<script>
import {
  listEvent,
  getEvent,
  delEvent,
  addEvent,
  updateEvent,
} from "@/api/alarm/event";
import dayjs from "dayjs";

import eventDetail from "./components/eventDetail";

export default {
  name: "Event",
  dicts: ["alarm_type"],
  components: { eventDetail },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警事件表格数据
      eventList: [],
      eventFinishOptions: [
        { dictValue: 0, dictLabel: "未结束" },
        { dictValue: 1, dictLabel: "已结束" },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        targetType: undefined,
        alarmType: undefined,
        deptId: undefined,
        personName: undefined,
        // carNum: undefined,
        eventFinish: undefined,
        daterange: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        eventId: [
          { required: true, message: "事件id不能为空", trigger: "blur" },
        ],
      },
      eventItem: {},
      eventDetailVisible: false,
    };
  },
  computed: {
    deptId() {
      return this.$store.getters.choosedDeptId;
    },
  },
  watch: {
    deptId: function () {
      this.queryParams.deptId = this.deptId;

      this.getList();
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报警事件列表 */
    getList() {
      this.loading = true;
      this.queryParams.deptId = this.deptId;

      if (
        this.queryParams.daterange &&
        this.queryParams.daterange.length == 2
      ) {
        this.queryParams.eventStart = dayjs(
          this.queryParams.daterange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.eventEnd = dayjs(this.queryParams.daterange[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      } else {
        this.queryParams.eventStart = null;
        this.queryParams.eventEnd = null;
      }
      let params = JSON.parse(JSON.stringify(this.queryParams));
      delete params.daterange;
      listEvent(params).then((response) => {
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.eventId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 查看按钮操作 */
    handleView(row) {
      const eventId = row.eventId || this.ids;
      getEvent(eventId).then((response) => {
        let tempData = response.data;
        tempData.eventFinishName = this.eventFinishOptions.find(
          (v) => v.dictValue == response.data.eventFinish
        ).dictLabel;
        tempData.alarmTypeName = this.dict.type.alarm_type.find(
          (v) => v.value == response.data.alarmType
        ).dictLabel;
        this.eventItem = tempData;
        this.eventDetailVisible = true;
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const eventIds = row.eventId || this.ids;
      this.$modal
        .confirm('是否确认删除"' + (row.orderName || "已勾选") + '"的数据项?')
        .then(() => {
          this.loading = true;
          return delEvent(eventIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "alarm/event/export",
        {
          ...this.queryParams,
        },
        `告警事件_${new Date().getTime()}.xlsx`
      );
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.buttonLoading = true;
        if (valid) {
          this.form.dealStatus = 2;
          updateEvent(this.form).then((response) => {
            this.buttonLoading = false;
            if (response.code === 200) {
              this.$modal.msgSuccess("处理成功");
              this.open = false;
              this.getList();
            }
          });
        }
      });
    },
  },
};
</script>
